﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Wild2.Models.User
{
    public class Invite
    {
        public static List<InviteData> GetInvites(int userID)
        {
            List<InviteData> invites = new List<InviteData>();
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(@"SELECT [Invites].[ID], [Invites].[Kto], [Invites].[Kogo], [Invites].[code], [Invites].[date], [Users].[Nick] 
                                                FROM [Invites] 
                                                LEFT JOIN [Users] ON [Invites].[Kogo] = [Users].[ID]
                                                WHERE [Invites].[Kto] = @UID
                                                ORDER BY [Invites].[date] DESC", conn);
                cmd.Parameters.Add("@UID", SqlDbType.Int);
                cmd.Parameters["@UID"].Value = userID;
                cmd.Connection.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    InviteData inviteData = new InviteData();
                    inviteData.ID = Convert.ToInt32(reader["id"]);
                    inviteData.Kto = Convert.ToInt32(reader["Kto"]);
                    if (reader["Kogo"] != DBNull.Value) inviteData.Kogo = Convert.ToInt32(reader["Kogo"]);
                    if (reader["Nick"] != DBNull.Value) inviteData.KogoNick = reader["Nick"].ToString();
                    inviteData.Invite = reader["code"].ToString();
                    inviteData.date = Convert.ToDateTime(reader["date"]);
                    invites.Add(inviteData);
                }
                reader.Close();
                cmd.Connection.Close();
            }
            return invites;
        }
    }
}